<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <link href="../lib/bootstrap.min.css" rel="stylesheet"/>
    <script src="../lib/jquery-2.2.4.js"></script>
    <script src="../lib/bootstrap.js"></script>
    <script src="../lib/vue.js"></script>
</head>
<body>
<div class="container" id="box">
    <form role="form">
        <div class="form-group">
            <label for="username">用户名:</label>
            <input v-model="username" id="username" type="text" class="form-control" placeholder="输入用户名"/>
        </div>
        <div class="form-group">
            <label for="age">年龄:</label>
            <input v-model="age" id="age" type="number" class="form-control" placeholder="输入年龄"/>
        </div>
        <div class="form-group">
            <input  @click="add" type="button" value="添加" class="btn btn-primary"/>
            <input @click="reset" type="button" value="重置" class="btn btn-danger"/>
        </div>
    </form>
    <hr/>
    <table class="table table-bordered table-hover">
        <caption class="h3 text-info text-center">用户信息表</caption>
        <tr class="text-danger">
            <th class="text-center">序号</th>
            <th class="text-center">名字</th>
            <th class="text-center">年龄</th>
            <th class="text-center">操作</th>
        </tr>
        <tr class="text-center" v-for="(item,index) in myData">
            <td>{{index+1}}</td>
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
            <td>
                <button class="btn btn-primary btn-sm"
                        data-toggle="modal" data-target="#layer" @click="deleteOne">删除</button>
            </td>
        </tr>
        <tr v-show="myData.length!=0">
            <td colspan="4" class="text-right">
                <button @click="deleteAll" data-toggle="modal" data-target="#layer"
                        class="btn btn-danger btn-sm">全部删除</button>
            </td>
        </tr>
        <tr v-show="myData.length==0">
            <td colspan="4" class="text-center text-muted">
                <p>暂无数据...</p>
            </td>
        </tr>
    </table>

    <!--模态框-->
    <div role="dialog" class="modal fade" id="layer">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span>&times;</span>
                    </button>
                    <h4 class="modal-title text-center">{{deleteTxt}}</h4>
                </div>
                <div class="modal-body text-right">
                    <button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button>
                    <button @click="remove()" data-dismiss="modal" class="btn btn-danger btn-sm">确认</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    window.onload = function () {
        new Vue({
            el: '#box',
            data: {
                myData: [],
                username: '',
                age: '',
                nowIndex: -100,
                deleteTxt: ''
            },
            methods: {
                add: function () {
                    if(!this.username){
                        alert("用户名不能为空");
                        return;
                    }
                    if(!this.age || this.age<=0){
                        alert("年龄不能为空");
                        return;
                    }
                    var item = {
                        name: this.username,
                        age: this.age
                    }
                    this.myData.push(item);
                    this.username = '';
                    this.age = '';
                },
                reset: function () {
                    this.username = '';
                    this.age = '';
                },
                deleteAll: function () {
                    this.nowIndex=-1;
                    this.deleteTxt = "确认删除全部吗?";
                },
                deleteOne: function (index) {
                    this.nowIndex = index;
                    this.deleteTxt = "确认删除吗?";
                },
                remove: function () {
                    if(this.nowIndex==-1){
                        this.myData = [];
                    }else{
                        this.myData.splice(this.nowIndex,1)
                    }

                }
            }
        });
    }
</script>
</body>
</html>